package com.fmistuff.chain.nodes;

import java.util.HashMap;
import java.util.Map;

public class ExprNode extends MarkovNode {

	public static final String HASHKEY = TermNode.class.getSimpleName();

	private static Map<String, MarkovNode> pool;
	private static final String PLUS = "+";
	private static final String MINUS = "-";

	static {
		pool = new HashMap<String, MarkovNode>();
		pool.put(PLUS, new ExprNode(PLUS));
		pool.put(MINUS, new ExprNode(MINUS));
		pool.put("term", new TermNode());

		pool.get(PLUS).setProbability(0.1f);
		pool.get(MINUS).setProbability(0.1f);

	}
	
	public ExprNode() {
		this(pickANode(pool).getData());
	}

	public ExprNode(String data) {
		this(data, 0.5f);
	}

	protected ExprNode(String data, float probability) {
		super(data, probability);
	}

	@Override
	public String descend() {
		MarkovNode currentNode = pickANode(pool);

		if (!isFinal()) {
			if (currentNode instanceof ExprNode) {
				final String data = getData();
				//reinitialize and descend
				return descend() + data + currentNode.descend();
			}

			else { // if it is a num node or the recursive node
				return currentNode.descend();
			}
		}

		return getData();
	}

	@Override
	public boolean isFinal() {
		return false;
	}

}
